package org.example.ui;

import javafx.animation.ScaleTransition;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;
import org.example.audio.AudioPlayer;


public class Main extends Application {

    private static final String ICON_SRC = "img/icon.png";
    private static final String TITLE = "JWoodBlock";
    private static final String ZOOM = "0.8";
    private static final String TRANSPARENT_STYLE = "-fx-background-color: transparent";

    private long lastClickTime = 0;

    private ImageView imageView;
    private Text clickCountText;

    private Text clickAverageText;

    private int clickCount;

    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();
        HBox dataMonitorHbox = new HBox();
        Scene scene = new Scene(root, 600, 400);

        // 加载图片
        imageView = new ImageView(ICON_SRC);

        // 添加工具栏和按钮
        Button fullButton = new Button("全屏");
        fullButton.setOnAction(event -> {
            Stage stage = (Stage) fullButton.getScene().getWindow();
            stage.setFullScreen(!stage.isFullScreen());
        });


        ToolBar toolBar = new ToolBar(fullButton);
        toolBar.setStyle(TRANSPARENT_STYLE);

        HBox topBox = new HBox(toolBar);
        topBox.setStyle(TRANSPARENT_STYLE);

        // 监听按键事件
        scene.setOnKeyPressed(event -> {
            if (event.getCode() == KeyCode.F11) {
                Stage stage = (Stage) fullButton.getScene().getWindow();
                stage.setFullScreen(!stage.isFullScreen());
            }
        });

        // 监听鼠标点击事件
        scene.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
            ScaleTransition scale = new ScaleTransition(Duration.seconds(0.1), imageView);
            scale.setToX(Double.parseDouble(ZOOM));
            scale.setToY(Double.parseDouble(ZOOM));
            clickCount++;
            clickCountText.setText("功德: " + clickCount + "  |  ");


            long currentTime = System.currentTimeMillis();
            if (lastClickTime == 0) {
                lastClickTime = currentTime;
                return;
            }
            long timeDiff = currentTime - lastClickTime;
            lastClickTime = currentTime;
            double clickSpeed = 60.0 / (double) timeDiff * clickCount;
            clickAverageText.setText(String.format("速度: 每分钟%.1f功德", clickSpeed));

            //音效
            new AudioPlayer().playSound();
            //文字
            scale.setOnFinished(event1 -> createAndStartReverseScaleTransition());
            scale.play();

            // 添加文字
            Text clickText = new Text("功德+1");
            root.getChildren().add(clickText);
            clickText.setFont(Font.font("Microsoft YaHei", FontWeight.BOLD, 25));

            clickText.setLayoutX((primaryStage.getWidth() - clickText.getBoundsInLocal().getWidth()) / 2);
            clickText.setLayoutY((primaryStage.getHeight() - clickText.getBoundsInLocal().getHeight()) / 2);

            TranslateTransition transition = new TranslateTransition(Duration.seconds(2), clickText);
            transition.setByY(-100);
            transition.setOnFinished(e -> root.getChildren().remove(clickText));

            Platform.runLater(transition::play);
        });

        // 创建主布局
        root.setTop(topBox);
        root.setCenter(imageView);


        clickCountText = new Text("功德: 0" + "  |  ");

        clickAverageText = new Text("速度: 每分钟0功德");
        dataMonitorHbox.getChildren().addAll(clickCountText, clickAverageText);
        root.setBottom(dataMonitorHbox);
        root.setPadding(new Insets(0, 5, 5, 5));

        root.setStyle(TRANSPARENT_STYLE);
        scene.setFill(Color.LIGHTBLUE);

        // 创建场景
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.getIcons().add(new Image(ICON_SRC));
        primaryStage.setTitle(TITLE);

        primaryStage.setFullScreen(false);
        // 显示窗口
        primaryStage.show();
    }

    private void createAndStartReverseScaleTransition() {
        ScaleTransition reverseScale = new ScaleTransition(Duration.seconds(0.1), imageView);
        reverseScale.setFromX(0.8);
        reverseScale.setFromY(0.8);
        reverseScale.setToX(1.0);
        reverseScale.setToY(1.0);
        reverseScale.play();
    }

    @Override
    public void stop() throws Exception {
        super.stop();
    }

    public static void main(String[] args) {
        launch(args);
    }
}